package com.project.shtp.controller;

import com.alibaba.fastjson.JSON;

import com.alibaba.fastjson.JSONObject;
import com.project.shtp.pojo.web.vo.*;
import com.project.shtp.pojo.web.dto.User;
import com.project.shtp.pojo.web.dto.PersonalInfoDTO;

import com.project.shtp.service.UserService;
import com.project.shtp.utils.CommonResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

//    @PostMapping("/login")
//    @ApiOperation("登录")
//    public CommonResult login(@RequestBody LoginVo loginVo, HttpServletRequest request)
//    {
//        return userService.login(loginVo, request);
//    }
//
//    @PostMapping("/register")
//    @ApiOperation("注册")
//    public CommonResult register(@RequestBody RegisterInfo registerInfo, HttpServletRequest request)
//    {
//        return userService.register(registerInfo, request);
//    }


    @PostMapping("/verificationCode")
    @ApiOperation("验证码请求")
    public CommonResult<Map> verificationCode(@RequestBody String data, HttpServletRequest request, HttpServletResponse response)
    {
        CommonResult<Map> mapCommonResult = userService.verificationCode(data, request, response);
        return mapCommonResult;
    }

    @PostMapping("/queryUserAllRole")
    @ApiOperation("查询用户所有身份")
    public CommonResult queryUserAllRole(@RequestBody String data) {
        Integer userId = Integer.valueOf(JSON.parseObject(data).getString("userId").toString());
        return userService.queryUserAllRole(userId);
    }

    @PostMapping("/queryMyOrderForm")
    @ApiOperation("查看我的订单")
    public CommonResult queryMyOrderForm(@RequestBody String data){
        int userId = Integer.parseInt(JSONObject.parseObject(data).getString("userId"));
        int statusId = Integer.parseInt(JSONObject.parseObject(data).getString("statusId"));
        List<OrderInquiry> orderInquiryList=userService.queryMyOrderForm(userId,statusId);
       return new CommonResult(200,"查询相应状态订单成功",orderInquiryList);
    }

    @PostMapping("/searchMyGoods")
    @ApiOperation("查看我的商品")
    public CommonResult searchMyGoods(@RequestBody  String data){
        int userId = Integer.parseInt(JSON.parseObject(data).getString("userId"));
        List<MyGoodsInfo> myGoodsInfos = userService.searchMyGoods(userId);
        return new CommonResult(200,"查询我的商品成功",myGoodsInfos);
    }


    @PostMapping("/queryFollower")
    @ApiOperation("查看用户关注的人")
    public CommonResult queryFollower(@RequestBody String data){
//        Integer userId = Integer.parseInt(data);
        int userId = Integer.parseInt(JSONObject.parseObject(data).getString("userId"));
        List<User> list = userService.queryFollower(userId);
        return new CommonResult(200,"请求成功",list);
    }

    @PostMapping("/queryHistory")
    @ApiOperation("查看用户历史浏览记录")
    public CommonResult queryHistory(@RequestBody String data){
        int userId = Integer.parseInt(JSONObject.parseObject(data).getString("userId"));
        List<HistoryBrowse> list = userService.queryUserBrowse(userId);
        return new CommonResult(200,"请求成功",list);
    }

    @PostMapping("/queryFans")
    @ApiOperation("查看用户粉丝")
    public CommonResult queryFans(@RequestBody String data){
        int userId = Integer.parseInt(JSONObject.parseObject(data).getString("userId"));
        List<User> list = userService.queryUserFans(userId);
        return new CommonResult(200,"请求成功",list);

    }

    @PostMapping("/changeOrder")
    @ApiOperation("确认和回退用户订单")
    public CommonResult changeOrder(@RequestBody String data){
        int orderId = Integer.parseInt(JSONObject.parseObject(data).getString("orderId"));
        int statusId = Integer.parseInt(JSONObject.parseObject(data).getString("statusId"));

        int i = userService.changeOrder(orderId,statusId);
        if (i > 0){
            return new CommonResult(200,"修改成功");
        }
        else
            return new CommonResult(500,"修改失败");
    }


    @PostMapping("/updatePersonalInfo")
    @ApiOperation("修改个人信息")
    public CommonResult updatePersonalInfo(@RequestParam("file") MultipartFile file, PersonalInfoDTO personalInfoDTO){
        return userService.updatePersonalInfo(file,personalInfoDTO);
    }

    @PostMapping("/queryUserAllMessage")
    @ApiOperation("查询用户个人所有信息")
    public CommonResult queryUserAllMessage(@RequestBody String data) {
        int userId  =Integer.parseInt(JSONObject.parseObject(data).getString("userId"));
        List<UserAllMessage> s1 = userService.queryUserAllMessage(userId);
        return new CommonResult(200,"返回成功",s1);
    }

    @PostMapping("/UserCollection")
    @ApiOperation("查询用户的收藏")
    public CommonResult UserCollection(@RequestBody String data) {
        int userId  =Integer.parseInt(JSONObject.parseObject(data).getString("userId"));
        List<UserCollection> list = userService.queryUserCollection(userId);
        return new CommonResult(200,"返回成功",list);
    }

    @PostMapping("/ViewOrderWithGoods")
    @ApiOperation("查看我的商品关联的订单")
    public CommonResult ViewOrderWithGoods(@RequestBody String data) {
        int userId  =Integer.parseInt(JSONObject.parseObject(data).getString("userId"));
        List<UserOrderWithGoods> list = userService.queryOrderWithGoods(userId);
        return new CommonResult(200,"返回成功",list);
    }



    @PostMapping( "/cancelTheOrder")
    @ApiOperation("取消我的订单")
    public CommonResult  cancelTheOrder(@RequestBody String data ){
        Integer orderId = Integer.valueOf(JSON.parseObject(data).getString("orderId").toString());
        userService.cancelTheOrder(orderId);
        return new CommonResult(200,"返回成功",orderId );
    }

}
